using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using SOEI.Solution.Application.Contracts.Customized.RoomSourceVerifyV1.Dtos;
using SOEI.Solution.DomainEntities.RoomSourceVerifiyV1.DomainService;
using SOEI.Solution.DomainEntities.RoomSourceVerifiyV1.Dtos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace SOEI.Solution.DomainEntities.RoomSourceVerifiyV1
{
    /// <summary>
    /// 【扩展模块】  <br/>
    /// 【功能描述】  ：XXX 模块<br/>
    /// 【创建日期】  ：2020.05.21 <br/>
    /// 【开发人员】  ：static残影<br/>
    ///</summary>
    [ApiExplorerSettings(GroupName = "Manager", IgnoreApi = true)]
    public class LicensesUploadAppService : SolutionAppServiceBase, ILicensesUploadAppService
    {
        /// <summary>
        ///【LicensesUpload】仓储层
        /// </summary>
        private readonly IRepository<LicensesUpload, string> _licensesuploadRepository;

        /// <summary>
        ///【LicensesUpload】领域服务
        /// </summary>
        private readonly ILicensesUploadManager _licensesuploadManager;

        public LicensesUploadAppService(
            IRepository<LicensesUpload, string> licensesuploadRepository,
            ILicensesUploadManager licensesuploadManager
        )
        {
            _licensesuploadRepository = licensesuploadRepository;
            _licensesuploadManager = licensesuploadManager;
        }

        #region -------------------------------------------------辅助工具生成---------------------------------------------- 

        /// <summary>
        ///【LicensesUpload】获取的分页列表信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize()]
        public async Task<PagedResultDto<LicensesUploadListDto>> GetPaged(GetLicensesUploadsInput input)
        {
            var query = _licensesuploadRepository.GetAll()
                          //模糊搜索 字段Id
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.Id.Contains(input.FilterText))
                          //模糊搜索 字段XM
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.XM.Contains(input.FilterText))
                          //模糊搜索 字段ZJLX
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.ZJLX.Contains(input.FilterText))
                          //模糊搜索 字段ZJHM
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.ZJHM.Contains(input.FilterText))
                          //模糊搜索 字段LXDH
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.LXDH.Contains(input.FilterText))
                          //模糊搜索 字段TXDZ
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.TXDZ.Contains(input.FilterText))
                          //模糊搜索 字段SFLBDM
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.SFLBDM.Contains(input.FilterText))
                          //模糊搜索 字段WTHTBH
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.WTHTBH.Contains(input.FilterText))
                          //模糊搜索 字段ZJLXDM
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.ZJLXDM.Contains(input.FilterText))
                          //模糊搜索 字段GZDW
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.GZDW.Contains(input.FilterText))
                          //模糊搜索 字段YB
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.YB.Contains(input.FilterText))
                          //模糊搜索 字段FBBH
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.FBBH.Contains(input.FilterText))
            ;
            // TODO:根据传入的参数添加过滤条件

            var count = await query.CountAsync();

            var entityList = await query
                    .OrderBy(input.Sorting).AsNoTracking()
                    .PageBy(input)
                    .ToListAsync();

            var entityListDtos = ObjectMapper.Map<List<LicensesUploadListDto>>(entityList);

            return new PagedResultDto<LicensesUploadListDto>(count, entityListDtos);
        }

        /// <summary>
        ///【LicensesUpload】通过指定id获取MemberListDto信息
        /// </summary>
        [AbpAuthorize()]
        public async Task<LicensesUploadListDto> GetById(EntityDto<string> input)
        {
            var entity = await _licensesuploadRepository.GetAsync(input.Id);

            var dto = ObjectMapper.Map<LicensesUploadListDto>(entity);
            return dto;
        }

        /// <summary>
        ///【Company】 获取编辑
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize()]
        public async Task<GetLicensesUploadForEditOutput> GetForEdit(EntityDto<string> input)
        {
            var output = new GetLicensesUploadForEditOutput();
            LicensesUploadEditDto editDto;

            if (!string.IsNullOrEmpty(input.Id))
            {
                var entity = await _licensesuploadRepository.GetAsync(input.Id);
                editDto = ObjectMapper.Map<LicensesUploadEditDto>(entity);
            }
            else
            {
                editDto = new LicensesUploadEditDto();
            }
            output.LicensesUpload = editDto;
            return output;
        }
        /// <summary>
        ///【LicensesUpload】 添加或者修改的公共方法
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize()]
        public async Task CreateOrUpdate(CreateOrUpdateLicensesUploadInput input)
        {
            if (!string.IsNullOrEmpty(input.LicensesUpload.Id))
            {
                await Update(input.LicensesUpload);
            }
            else
            {
                await Create(input.LicensesUpload);
            }
        }
        /// <summary>
        ///【LicensesUpload】新增
        /// </summary>
        [AbpAuthorize()]
        protected virtual async Task<LicensesUploadEditDto> Create(LicensesUploadEditDto input)
        {
            //TODO:新增前的逻辑判断，是否允许新增

            var entity = ObjectMapper.Map<LicensesUpload>(input);
            //调用领域服务
            entity = await _licensesuploadManager.CreateAsync(entity);

            var dto = ObjectMapper.Map<LicensesUploadEditDto>(entity);
            return dto;
        }

        /// <summary>
        ///【LicensesUpload】编辑
        /// </summary>
        [AbpAuthorize()]
        protected virtual async Task Update(LicensesUploadEditDto input)
        {
            //TODO:更新前的逻辑判断，是否允许更新
            var key = input.Id;
            var entity = await _licensesuploadRepository.GetAsync(key);
            //  input.MapTo(entity);
            //将input属性的值赋值到entity中
            ObjectMapper.Map(input, entity);
            await _licensesuploadManager.UpdateAsync(entity);
        }

        /// <summary>
        ///【LicensesUpload】删除信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize()]
        public async Task Delete(EntityDto<string> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _licensesuploadManager.DeleteAsync(input.Id);
        }

        /// <summary>
        ///【LicensesUpload】 批量删除Member的方法
        /// </summary>
        [AbpAuthorize()]
        public async Task BatchDelete(List<string> input)
        {
            // TODO:批量删除前的逻辑判断，是否允许删除
            await _licensesuploadManager.BatchDelete(input);
        }

        #endregion

        #region -------------------------------------------------用户自定义------------------------------------------------
        /*请在此扩展应用服务实现*/

        /// <summary>
        ///【LicensesUpload】 证照上传保存
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize()]
        public async Task<LicensesUploadSaveEditDto> LicensesUploadSave(CreateOrUpdateLicensesUploadSaveInput input)
        {
            LicensesUploadEditDto licensesUploadEditDto;

            if (input != null)
            {
                licensesUploadEditDto = ObjectMapper.Map<LicensesUploadEditDto>(input.LicensesUploadSave);

                if (!string.IsNullOrEmpty(licensesUploadEditDto.Id))
                {
                    await Update(licensesUploadEditDto);

                    return input.LicensesUploadSave;
                }
                else
                {

                    string logicId = Guid.NewGuid().ToString("N");

                    logicId = "A0" + logicId.Substring(logicId.Length - 8, logicId.Length - (logicId.Length - 8));

                    licensesUploadEditDto.Id = logicId;

                    var result = await Create(licensesUploadEditDto);

                    return ObjectMapper.Map<LicensesUploadSaveEditDto>(result);
                }
            }

            return null;
        }

        #endregion
    }
}
